package me.cxlr.qinlauncher2.view.settings;

import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.preference.DropDownPreference;
import androidx.preference.ListPreference;
import androidx.preference.PreferenceFragmentCompat;

import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.List;

import me.cxlr.qinlauncher2.R;
import me.cxlr.qinlauncher2.manager.IconManager;
import me.cxlr.qinlauncher2.manager.SoftwareManager;
import me.cxlr.qinlauncher2.model.IconSelect;
import me.cxlr.qinlauncher2.util.SharedPreferencesUtil;
import me.cxlr.qinlauncher2.util.ThemeUtil;

/**
 * @author 酷安用户:山轮纸
 */
public class BaseSettingsFragment extends PreferenceFragmentCompat {
    private ListPreference theme;
    private DropDownPreference iconPackage;
    private ListPreference screenOrientation;

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences_base_settings, rootKey);

        // 主题
        theme = findPreference("theme");
        assert theme != null;
        theme.setOnPreferenceChangeListener((preference, newValue) -> {
            Snackbar.make(requireView(), "启动器即将重新启动", Snackbar.LENGTH_LONG).show();
            Intent intent = requireActivity().getPackageManager().getLaunchIntentForPackage(requireActivity().getPackageName());
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        });

        // 图标包
        iconPackage = findPreference("select_icon_package");
        String iconPackageName = SharedPreferencesUtil.getInstance().getString("select_icon_package", "system");
        String iconPackageLabel = SharedPreferencesUtil.getInstance().getString("select_icon_package_label", "系统默认");
        iconPackage.setSummary(iconPackageLabel);
        iconPackage.setOnPreferenceClickListener(preference -> {
            dialogSelectIconPackage();
            return true;
        });

        // 屏幕方向
        screenOrientation = findPreference("screen_orientation");
        assert screenOrientation != null;
        screenOrientation.setEnabled(false);
        screenOrientation.setOnPreferenceChangeListener((preference, newValue) -> {
            Snackbar.make(requireView(), "返回桌面后生效", Snackbar.LENGTH_SHORT).show();
            return true;
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        ThemeUtil.initTheme();
    }

    private void dialogSelectIconPackage() {
        List<IconSelect> iconPackageList = new ArrayList<>(16);
        iconPackageList.add(IconSelect.builder().packageName("system").label("系统默认").build());
        List<IconSelect> thirdIconPackageList = SoftwareManager.getInstance().getIconPackageApplicationList();
        iconPackageList.addAll(thirdIconPackageList);
        String[] entries = new String[iconPackageList.size()];
        for (int i = 0; i < entries.length; i++) {
            entries[i] = iconPackageList.get(i).getLabel();
        }
        String[] entryValues = new String[iconPackageList.size()];
        for (int i = 0; i < entryValues.length; i++) {
            entryValues[i] = iconPackageList.get(i).getPackageName();
        }
        AlertDialog alertDialog = new AlertDialog.Builder(requireContext())
                .setTitle("选择图标包")
                .setItems(entries, (dialog, which) -> {
                    SharedPreferencesUtil.getInstance().setString("select_icon_package", entryValues[which]);
                    SharedPreferencesUtil.getInstance().setString("select_icon_package_label", entries[which]);
                    iconPackage.setSummary(entries[which]);
                    IconManager.getInstance().reloadIconPackage();
                    dialog.dismiss();
                })
                .setPositiveButton("关闭", (dialog, which) -> dialog.dismiss())
                .create();
        alertDialog.show();
    }
}